package com.banyuan.Utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Date;
import java.util.Properties;

public class JDBCUtils {
    static String url;
    static String name;
    static String pw;

    static {
        InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        try {
            properties.load(is);
            url = properties.getProperty("url");
            name = properties.getProperty("name");
            pw = properties.getProperty("pw");
            String driverClassName = properties.getProperty("driverClassName");
            Class.forName(driverClassName); //加载这个类，去执行这个类里面的静态代码块，即Driver
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }



public static Connection getConnection() throws SQLException {
    Connection conn = DriverManager.getConnection(url, name, pw);
    return conn;
    // 写了一个获取连接------getConnection的方法
}




    public static ResultSet executeQuery(String sql,Object ...obj) throws SQLException {
        Connection conn = getConnection();
        ResultSet rs = executeQuery(conn, sql, obj);
        return rs;
    }


public static ResultSet executeQuery(Connection conn,String sql,Object ...obj) throws SQLException {
        PreparedStatement pstm = null;
        pstm = conn.prepareStatement(sql);
        for (int i = 0; i < obj.length; i++) {
            pstm.setObject(i+1,obj[i]);
        }
        ResultSet rs = pstm.executeQuery();
        return rs;

}



    public static int executeUpdate(String sql,Object ...obj) throws SQLException {
        Connection conn = getConnection();
        try {
            return executeUpdate(conn, sql, obj);
        } finally {
        close(conn);
        }

    }




public static int executeUpdate(Connection conn,String sql,Object ...obj) throws SQLException {
        PreparedStatement pstm = null;
    try {
        pstm = conn.prepareStatement(sql);
        for (int i = 0; i < obj.length; i++) {
            pstm.setObject(i+1,obj[i]);
        }

        return pstm.executeUpdate();
    }  finally {
        close(pstm);
    }
}









    // 关流

    public static void close(AutoCloseable autoCloseable) {
        if (autoCloseable != null) {
            try {
                autoCloseable.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    public static void closeAllByRs(ResultSet rs) {
        if (rs != null) {
            try {
                Statement statement = rs.getStatement();
                Connection connection = statement.getConnection();
                JDBCUtils.close(rs);
                JDBCUtils.close(statement);
                JDBCUtils.close(connection);

            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        }
    }


}
